package server

import (
	"encoding/base64"
	"log"
	"net/http"
	"sync"

	"github.com/corego/protoTrans/config"
	"github.com/labstack/echo"
)

var b64 *base64.Encoding
var mux = &sync.RWMutex{}

func Start() {

	b64 = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
	// initLocalDB()
	// loadDataFromLocalDB()
	initServer()
	gConsumer = NewConsumerKafka()
	gConsumer.Start()
	initKafka()

	initPprof()

	log.Println(config.Conf)
}

func Close() {
	// stop kafka consumer
	if gConsumer != nil {
		gConsumer.Close()
	}
}

// 启动注册服务
func initServer() {
	e := echo.New()
	e.POST("/route", route)
	e.POST("/chat/mqtt", mqttChat)
	// 公共API
	go e.Start(config.Conf.Proto.Addr)
}

func initPprof() {
	go func() {
		log.Println(http.ListenAndServe(":6064", nil))
	}()

}
